package com.xxxx.paipaicars.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;

/**
 * redis配置文件
 */
@Configuration
public class RedisConfig {


    @Bean
    public StringRedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        // 创建一个redisTemplate对象
        StringRedisTemplate result = new StringRedisTemplate();
        // 设置连接工厂
        result.setConnectionFactory(redisConnectionFactory);
        // spring内置的序列化转换器
        GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
        // 设置key的序列化方式
        result.setKeySerializer(RedisSerializer.string());
        // 设置hash key的序列化方式
        result.setHashKeySerializer(RedisSerializer.string());

        // 设置值的序列化方式
        result.setValueSerializer(genericJackson2JsonRedisSerializer);
        // 设置hash value的序列化方式
        result.setHashValueSerializer(genericJackson2JsonRedisSerializer);
        result.afterPropertiesSet();
        return result;
    }


}